package com.air.sso.securityoauth2.config.service;


import com.air.sso.securityoauth2.pojo.Permission;
import com.air.sso.securityoauth2.pojo.User;
import com.air.sso.securityoauth2.service.impl.PermissionServiceImpl;
import com.air.sso.securityoauth2.service.impl.UserServiceImpl;
import org.assertj.core.util.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserServiceImpl userService;

    @Resource
    private PermissionServiceImpl permissionService;


    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getById(37);

        List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();
        if (user != null) {
            List<Permission> permissionList = permissionService.list();

            permissionList.forEach(item -> {
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(item.getEnname());
                grantedAuthorities.add(grantedAuthority);
            });


        }

        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
    }
}
